import 'package:flame/components.dart';
import 'package:flutter_flame/worlds/game/GameWorld.dart';
import 'package:flutter_flame/worlds/game/model/PlantModel.dart';

import '../../../bloc/game_bloc/GameEvent.dart';
import '../entity/SpriteAnimationParamEntity.dart';
import 'plants/Plant.dart';
import 'Sun.dart';
import 'LawnCell.dart';

class Sunflower extends Plant {
  LawnCell lawnCell;

  Sunflower(this.lawnCell, {required double maxHp})
      : super(
          cell: lawnCell,
          hp: maxHp,
          initEntity: SpriteAnimationParamEntity.sunflowerIdle,
          plantModel: PlantModel.sunflower,
        );

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    add(
      TimerComponent(
        period: sunflowerGenerateSunPeriod,
        repeat: true,
        onTick: () => _produceSun(),
      ),
    );
  }

  // 生产阳光
  void _produceSun() {
    world.add(
      SunLight(
        pos: position + Vector2(0, -50),
        onCollected: (world) => addSunlight(sunValue, world),
      ),
    );
  }

  void addSunlight(int value, GameWorld world) {
    world.bloc.add(CollectSunlightEvent(value));
  }
}
